package net.stogi.common.gwt.gxt.client.core.util;

import java.util.ArrayList;
import java.util.List;

import net.stogi.common.gwt.gxt.client.core.model.Model;
import net.stogi.common.mvp.core.model.KeyValues;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;

public class WidgetUtils {

	public static final String DEFAULT_MODEL_OBJECT_NAME = "__bean";

	private WidgetUtils() {}

	public static <K> void populateStoreWithModel(ListStore<ModelData> store, List<KeyValues<K>> modelList) {
		store.removeAll();
		List<ModelData> rowList = new ArrayList<ModelData>();
		for (KeyValues<K> model : modelList) {
			ModelData modelData = new Model();
			modelData.set("__bean", model);
			for (String param : model.getParams()) {
				modelData.set(param, model.getValue(param));
			}
			rowList.add(modelData);
		}
		store.add(rowList);
	}

	public static <K> ModelData convertModelToModelData(KeyValues<K> model) {
		ModelData modelData = new Model();
		modelData.set(DEFAULT_MODEL_OBJECT_NAME, model);
		for (String param : model.getParams()) {
			modelData.set(param, model.getValue(param));
		}
		return modelData;
	}
}
